﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace 历史曲线
{
    public partial class Form1 : Form
    {
        private string[] data;
        private string[] files;
        private int select_file = 0;
        public Form1(String[] args)
        {
            InitializeComponent();
            string path = args[0]; // 获取第一个参数作为路径
            if (Directory.Exists(path))
            {
                files = Directory.GetFiles(path);
                Console.WriteLine("Files in directory: " + path);
                for(int i=0;i<files.Length;i++)
                {
                    Console.WriteLine(Path.GetFileName(files[i])); // 只输出文件名
                    select_file = i;
                }
                if (File.Exists(files[select_file]))
                {
                    data = readandtime(files[select_file]);
                    this.Text = files[select_file];
                }
            }
            else
            {
                Console.WriteLine("The specified path does not exist: " + path);
            }
        }
        static string ExtractQuotedContent(string str)
        {
            int startIndex = str.IndexOf('"') + 1; // 获取第一个引号后的位置
            int endIndex = str.LastIndexOf('"'); // 获取最后一个引号的位置
            if (startIndex > 0 && endIndex > startIndex)
            {
                return str.Substring(startIndex, endIndex - startIndex);
            }

            return string.Empty; // 如果没有找到有效的引号内容，则返回空字符串
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            String[] lines = data[0].Split('~');
            foreach(String line in lines)
            {
                comboBox1.Items.Add(line);
                comboBox2.Items.Add(line);
            }
            comboBox1.SelectedIndex = 3;
            comboBox2.SelectedIndex = 31;
            comboBox1.SelectedIndexChanged += ComboBox1_SelectedIndexChanged;
            comboBox2.SelectedIndexChanged += ComboBox2_SelectedIndexChanged;
            comboBox1.SelectedIndex = 3;
            rebush_ui(comboBox1.SelectedIndex, comboBox2.SelectedIndex);
        }
        private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                label1.Text = comboBox1.SelectedIndex.ToString();
                rebush_ui(comboBox1.SelectedIndex, comboBox2.SelectedIndex);
            }
            catch (Exception) { }
        }
        private void ComboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                label2.Text = comboBox2.SelectedIndex.ToString();
                rebush_ui(comboBox1.SelectedIndex, comboBox2.SelectedIndex);
             }
            catch (Exception) { }
        }

        private void rebush_ui(int i1,int i2)
        {
            int length = data.Length;
            float[] data_value1 = new float[length];
            float[] data_value2 = new float[length];
            DateTime[] times = new DateTime[length];
            for (int i = 0; i < length; i++)
            {
                String line = data[i];
                String time =line.Substring(0, 18);
                string extractedContent = ExtractQuotedContent(line);
                string[] var = extractedContent.Split('~');
                DateTime time1 = new DateTime();
                if (DateTime.TryParse(time, out time1))
                {
                  //  Console.WriteLine(time1.ToString() + " " + var[3] + " " + var[31]);
                    times[i] = time1;
                    data_value1[i] = float.Parse(var[i1]);
                    data_value2[i] = float.Parse(var[i2]);
                }

            }
            hslCurveHistory1.SetLeftCurve("data_value1", data_value1, Color.Red);
            hslCurveHistory1.SetRightCurve("data_value2", data_value2, Color.Gray);
            hslCurveHistory1.ValueMaxLeft = data_value1.Max();
            hslCurveHistory1.ValueMinLeft = data_value1.Min();
            hslCurveHistory1.ValueMaxRight = data_value2.Max();
            hslCurveHistory1.ValueMinRight = data_value2.Min();
            hslCurveHistory1.SetDateTimes(times);
            hslCurveHistory1.RenderCurveUI();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int newselect = select_file - 1;
            if (newselect>0)
            {
                data = readandtime(files[newselect]);
                select_file= newselect;
                this.Text = files[select_file];
                rebush_ui(comboBox1.SelectedIndex, comboBox2.SelectedIndex);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int newselect = select_file +1;
            if (newselect<files.Length)
            {
                data = readandtime(files[newselect]);
                select_file= newselect;
                this.Text = files[select_file];
                rebush_ui(comboBox1.SelectedIndex, comboBox2.SelectedIndex);
            }
        }
        private String[] readandtime(String file)
        {
            string[] lines = File.ReadAllLines(file);
            string dateFromFileName = Path.GetFileNameWithoutExtension(file);
            for (int i = 0; i < lines.Length; i++)
            {
                lines[i] = $"{dateFromFileName} {lines[i]}";
            }
            return lines;
        }
        private void button3_Click(object sender, EventArgs e)
        {
            List<string> array = new List<string>();
            foreach (string file in files)
            {
                if (File.Exists(file))
                {
                    array.AddRange(readandtime(file));
                }
                else
                {
                    Console.WriteLine($"File not found: {file}");
                }
            }
            data = array.ToArray() ;
            rebush_ui(comboBox1.SelectedIndex, comboBox2.SelectedIndex);
        }
    }
}
